package com.ruoyi.io;

import java.io.*;
public class FileOutputStreamExample {
    public static void main(String[] args) {
        // 要写入的文件路径和名称
        String filePath = "E:\\output.txt";
        // 要写入文件的内容
        String content = "Hello, World!";
        // 创建输出流对象
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(filePath);
            // 将字符串转换为字节数组，并将其写入文件
            fos.write(content.getBytes("UTF-8"));
            // 刷新输出流
            fos.flush();
            // 输出提示信息
            System.out.println("Content has been written to " + filePath);
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + filePath);
        } catch (IOException e) {
            System.out.println("Error writing file: " + e.getMessage());
        } finally {
            // 关闭输出流
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing file: " + e.getMessage());
            }
        }
    }
}
